import { getTaiwuGong, getTaiwuTech } from './apiSvc'

export interface Kongfu {
    title: string
    mpai: string
    type: string
    lv: string
    lvNo: number
    wuxing: string
    obverse: string
    reverse: string
    slot: string
    required: string
    poison: string
    debuff: string
}

export type Men =
    | '少林派'
    | '峨眉派'
    | '百花谷'
    | '武当派'
    | '元山派'
    | '狮相门'
    | '然山派'
    | '璇女派'
    | '铸剑山庄'
    | '空桑派'
    | '金刚宗'
    | '五仙教'
    | '界青门'
    | '伏龙坛'
    | '血犼教'

export const allMen: Men[] = [
    '少林派',
    '峨眉派',
    '百花谷',
    '武当派',
    '元山派',
    '狮相门',
    '然山派',
    '璇女派',
    '铸剑山庄',
    '空桑派',
    '金刚宗',
    '五仙教',
    '界青门',
    '伏龙坛',
    '血犼教',
]

export const dMenpai =
    '少林派.3 峨眉派.4 百花谷.6 武当派.1 元山派.7 狮相门.3 然山派.4 璇女派.6 铸剑山庄.1 空桑派.7 金刚宗.3 五仙教.4 界青门.6 伏龙坛.1 血犼教.7'
        .split(' ')
        .map((e) => e.split('.') as [Men, string])
export const dTypes = '内功 身法 绝技 拳掌 腿法 指法 暗器 剑法 刀法 长兵 奇门 软兵 御射 乐器'.split(' ')
export const dWuxing = Object.fromEntries(
    '混元.8 金刚.3 归元.7 纯阳.1 紫霞.4 玄阴.6'.split(' ').map((e) => e.split('.')),
)
export const dLv = '神一 绝二 超三 极四 密五 奇六 上七 中八 下九'.split(' ').map((e) => e.split(''))

export const mMen2Wux = Object.fromEntries([
    ['少林派', '金刚'],
    ['峨眉派', '紫霞'],
    ['百花谷', '玄阴'],
    ['武当派', '纯阳'],
    ['元山派', '归元'],
    ['狮相门', '金刚'],
    ['然山派', '紫霞'],
    ['璇女派', '玄阴'],
    ['铸剑山庄', '纯阳'],
    ['空桑派', '归元'],
    ['金刚宗', '金刚'],
    ['五仙教', '紫霞'],
    ['界青门', '玄阴'],
    ['伏龙坛', '纯阳'],
    ['血犼教', '归元'],
])

export async function getKongfuList(document: Document): Promise<Kongfu[]> {
    const data = await getTaiwuGong()
    function getText(node: Element | undefined): string {
        return node?.textContent || ''
    }
    const all = data
        .map((e) => {
            const node = document.createElement<'div'>('div')
            node.innerHTML = e.html
            const pp = [...node.querySelectorAll('p')]
            const p0txt = getText(pp[0])
            const title = node.querySelector('.tt-title')?.textContent
            const mpai = allMen.find((e) => p0txt.startsWith(e[0]))!
            let ii = p0txt.indexOf('·')
            const lv = p0txt.slice(ii - 1, ii + 2)
            const lvNo = dLv.map((e) => e[1]).indexOf(lv.slice(-1)) + 1
            const wuxing = Object.keys(dWuxing).find((e) => p0txt.includes(e[0]))!
            const type = dTypes.find((e) => p0txt.includes(e))!
            ii = pp.findIndex((e) => getText(e).includes('心法正练'))
            const obverse = pp[ii + 1].textContent
            ii = pp.findIndex((e) => getText(e).includes('心法逆练'))
            const reverse = pp[ii + 1].textContent
            ii = pp.findIndex((e) => getText(e).includes('功法栏位：'))
            const slot = getText(pp[ii])
            ii = pp.findIndex((e) => getText(e).includes('施展需求：'))
            const required = getText(pp[ii])
            const [, extNode] = [...node.querySelectorAll('ul')]
            let poison = ''
            let debuff = ''
            if (extNode) {
                const [l1, l2] = [...extNode.querySelectorAll('li')]
                if (l1 && l2) {
                    poison = getText(l2)
                    debuff = getText(l1)
                } else {
                    if (l1) {
                        const tt = getText(l1)
                        if (tt.includes('毒')) {
                            poison = tt
                        } else {
                            debuff = tt
                        }
                    }
                }
            }
            node.remove()
            return {
                title,
                mpai,
                lv,
                lvNo,
                wuxing,
                type,
                obverse,
                reverse,
                slot,
                required,
                poison,
                debuff,
            } as Kongfu
        })
        .sort((a, b) => b.lvNo - a.lvNo)
    return all
}

export async function getTechs() {
    const lines = await getTaiwuTech()
    const regex = /《(.*?)》/g
    return lines.map((line) => {
        const kind = line.slice(0, 2)
        const books = Array.from(line.slice(2).matchAll(regex), (m) => m[1])
        return { kind, titles: books?.map((e) => e) }
    })
}
